package com.example.origin.finance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.origin.finance.dto.XueqiuKline;
import com.example.origin.finance.enums.TableNameEnum;
import com.example.origin.finance.service.AbstractMultiStoreService;
import com.example.origin.resttemplate.util.RestUtil;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Service
public class XueqiuKlineMultiStoreService extends AbstractMultiStoreService<XueqiuKline> {

    @Override
    protected void fillMap(String code, MultiValueMap<String, XueqiuKline> linkedMultiValueMap) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("begin", System.currentTimeMillis() + "");
        paramMap.put("period", "week");
        paramMap.put("type", "before");
        paramMap.put("count", "" + -720);
        paramMap.put("indicator", "kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance");

        Map<String, String> headerMap = new HashMap<>();
        String xueqiuCookie = getXueqiuCookie();
        headerMap.put("Cookie", xueqiuCookie);
        String u1 = "https://stock.xueqiu.com/v5/stock/chart/kline.json";


        if (!setParamMap(code, paramMap)) {
            return;
        }
        ResponseEntity<Object> response = RestUtil.restGet(restTemplate, u1, paramMap, headerMap, ParameterizedTypeReference.forType(Map.class));

        JSONObject x1 = JSONUtil.parseObj(response);


        JSONObject x2 = x1.getJSONObject("body").getJSONObject("data");
        JSONArray x3 = x2.getJSONArray("item");
        JSONArray x4 = x2.getJSONArray("column");
        String x5 = x2.get("symbol", String.class);

        if (x4 != null && x3 != null && !x4.isEmpty() && !x3.isEmpty()) {

            Map<Integer, String> x10 = new HashMap<>();
            for (int i = 0; i < x4.size(); i++) {
                x10.put(i, x4.getStr(i));
            }

            List<JSONArray> x8 = x3.toList(JSONArray.class);
            for (int i = 0; i < x8.size(); i++) {
                JSONArray x9 = x8.get(i);
                Map<String, Object> x11 = new HashMap<>();
                for (int j = 0; j < x9.size(); j++) {
                    Object o = x9.get(j);
                    String s = x10.get(j);
                    x11.put(s, o);
                }
                XueqiuKline x12 = BeanUtil.mapToBean(x11, XueqiuKline.class, false, CopyOptions.create());
                x12.setCode(code);
                x12.setSymbol(x5);
                linkedMultiValueMap.add(x12.getCode(), x12);
            }
        }
    }

}
